package com.mycloud.controller;

import com.mycloud.client.ProductClient;
import com.mycloud.dataobject.ProductInfo;
import com.mycloud.dto.CartDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * Created by PC on 2019/1/3.
 */
@RestController
@Slf4j
public class ClientController {

/*    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate restTemplate;*/

    @Autowired
    private ProductClient productClient;

    @GetMapping("/getProductMsg")
    public String getMsg() {


//        RestTemplate restTemplate = new RestTemplate();
        //第一种方式（写死url）
//        String forObject = restTemplate.getForObject("http://localhost:8080/msg", String.class);


        //第二种方式（利用loadBalancerClient获取url）
     /*   ServiceInstance product = loadBalancerClient.choose("PRODUCT");
        String url = String.format("http://%s%s", product.getHost(), product.getPort()) + "/msg";
        String forObject = restTemplate.getForObject(url, String.class);
*/
       //第三种方式
//        String forObject = restTemplate.getForObject("http://PRODUCT/msg", String.class);

        String forObject = productClient.getMsg();
        log.info("======================="+forObject);
        return forObject;
    }

    @GetMapping("/listPro")
    public List<ProductInfo> listPro() {
        List<ProductInfo> productInfos = productClient.listProductByIdList(Arrays.asList("157875196366160022"));
        return productInfos;
    }

    @GetMapping("/dStock")
    public void dStock() {
        productClient.decreaseStock(Arrays.asList(new CartDTO("157875227953464068",7)));
    }
}
